#include "PlayerMgr.h"


CPlayer::CPlayer( uint32 nUserID )
{
    m_nUserID = nUserID;
}

uint32 CPlayer::GetUserID()
{
    return m_nUserID;
}

CPlayerMgr::CPlayerMgr(void)
{
}


CPlayerMgr::~CPlayerMgr(void)
{
}

CPlayer * CPlayerMgr::AddPlayer( uint64 nProxyID, uint32 nUserID )
{
    CPlayer * pPlayer = NULL;
    TPlayerList::iterator i = m_playerList.find(nProxyID);
    if (i == m_playerList.end())
    {
        pPlayer = _CreatePlayer(nProxyID, nUserID);
    }
    else
    {
        pPlayer = i->second;
        if (pPlayer->GetUserID() != nUserID)
        {
            Remove(nProxyID);
            pPlayer = _CreatePlayer(nProxyID, nUserID);
        }
    }
    return pPlayer;
}

void CPlayerMgr::Remove( uint64 nProxyID )
{
    m_playerList.erase(nProxyID);
}

CPlayer * CPlayerMgr::Find( uint64 nProxyID )
{
    auto i = m_playerList.find(nProxyID);
    if (i != m_playerList.end())
        return i->second;
    else
        return NULL;
}

CPlayer * CPlayerMgr::_CreatePlayer( uint64 nProxyID, uint32 nUserID )
{
    CPlayer * pPlayer = m_playerPool.construct(nUserID);
    m_playerList[nProxyID] = pPlayer;
    return pPlayer;
}

